package com.bosssoft.cloud.wechat.common.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.bosssoft.cloud.wechat.common.bean.user.UserInfoDTO;
import com.bosssoft.cloud.wechat.common.bean.user.UserInfoExpVO;
import com.bosssoft.cloud.wechat.common.bean.user.UserInfoVO;
import com.bosssoft.cloud.wechat.common.infra.handler.SmsHandler;
import com.bosssoft.cloud.wechat.common.infra.util.UserSessionUtil;
import com.bosssoft.cloud.wechat.common.service.IUserService;
import com.bosssoft.cloud.wechat.infrastructure.annotation.VerifyCode;
import com.bosssoft.cloud.wechat.infrastructure.bean.response.R;
import com.bosssoft.cloud.wechat.infrastructure.constant.VerifyCodeConsts;
import com.bosssoft.cloud.wechat.infrastructure.enums.ErrEnum;
import com.bosssoft.cloud.wechat.infrastructure.enums.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户账户控制器
 *
 * @author wcy create on 2021/6/29 17:05 下午
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "UserController", tags = "用户相关api")
public class UserController {

    @Resource
    private SmsHandler smsHandler;
    @Autowired
    private IUserService userService;

    /**
     * 微信手机号注册
     */
    @ApiOperation(value = "微信用手机号注册")
    @GetMapping(value = "registByPhone")
    @VerifyCode(type = VerifyCodeConsts.PHONE, others = "phoneNumber")
    public R<?> registByPhone(@RequestParam @ApiParam("手机号") String phoneNumber,
                              @RequestParam @ApiParam("微信openid") String openid,
                              HttpSession session) {
        // 注册并获得用户信息
        UserInfoDTO userInfoDTO = userService.registByPhone(phoneNumber, openid);
        // 用户信息添加到session
        UserSessionUtil.setUserInfo(session, userInfoDTO);
        return new R<>(ResultEnum.SUCCESS, BeanUtil.toBean(userInfoDTO, UserInfoVO.class));
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping(value = "queryUserInfo")
    public UserInfoVO sendPhoneCode(HttpSession session) {
        return BeanUtil.toBean(UserSessionUtil.getUserInfo(session), UserInfoVO.class);
    }

    @ApiOperation(value = "发送手机验证码")
    @GetMapping(value = "sendPhoneCode")
    public R<?> sendPhoneCode(@RequestParam @ApiParam("要发送的手机号") String phoneNumber) {
        smsHandler.send(phoneNumber);
        return new R<>(ResultEnum.SEND_SUCCESS);
    }

    /**
     * @param wxCode 用于获取用户的openID，供之后发送公众号消息使用
     */
    @ApiOperation(value = "通过wxCode查看用户是否存在")
    @GetMapping(value = "getUserBywxCode")
    public R<?> getUserBywxCode(@RequestParam @ApiParam("微信标识码") String wxCode) {
        UserInfoExpVO expVO = BeanUtil.toBean(userService.getUserBywxCode(wxCode), UserInfoExpVO.class);
        if (StrUtil.isEmpty(expVO.getPhone()))
            return new R<>(ErrEnum.C0114, expVO);
        expVO.setExist(true);
        return new R<>(ResultEnum.QUERY_SUCCESS, expVO);
    }

    @ApiOperation(value = "解绑手机号")
    @GetMapping(value = "untyingPhone")
    @VerifyCode(type = VerifyCodeConsts.PHONE, others = "phoneNumber")
    public R<?> untyingPhone(@RequestParam @ApiParam("手机号") String phoneNumber,
                             @RequestParam @ApiParam("微信openid") String openid) {
//        smsHandler.verifyCode(phoneNumber, code);
        userService.untyingPhone(phoneNumber, openid);
        return new R<>(ResultEnum.UNBIND_SUCCESS);
    }
}

